//
//  Module_View.m
//  SecondPage
//
//  Created by winston on 13-4-11.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "Module_View.h"
#import "../../ResourceLoader/ResourceLoader/TemplateView.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"
#import "../../ResourceLoader/ResourceLoader/TemplateFactory.h"

@implementation Module_View

@synthesize autoID;
@synthesize moduleKey;

- (id)initWithFrame:(CGRect)frame autoID:(NSString*)_autoID ModuleID:(NSString *)_moduleID
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        [self setBackgroundColor:[UIColor clearColor]];
        self.clipsToBounds = NO;
        UIImageView * background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,1024, 768)];
        [self addSubview:background];
        [background release];
        [background setImage:[UIImage imageNamed:@"AllAuto_Image_BG"]];

        self.autoID = _autoID;
        self.moduleKey = _moduleID;
        
        int width = (TemplateSpaceX+TemplateWidth)*3;
        int height = (TemplateSpaceY+TemplateHeight)*2+TemplateTopSpace;
        
        NSString * rootpath = [self GetRootPath];
        NSArray * array = [self GetFiles:rootpath];
        
        timelineView = [[TimeLineView alloc] initWithFrame:CGRectMake(12, 633, 1000, 80)];
        [self addSubview:timelineView];
        [timelineView release];
        timelineView.alpha = 0;
        
        transView = [[TransmutationView alloc] initWithFrame:CGRectMake((frame.size.width-width)/2, (frame.size.height-height)/2, width, height) RootPath:rootpath Files:array];
        [self addSubview:transView];
        [transView release];
        transView.parentView = self;
        [transView renderTemplate];
        
        [transView addObserver:self forKeyPath:@"currentMiddleView" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	//实现相关操作
	UIView * currentview = [change objectForKey:NSKeyValueChangeNewKey];
    if (currentview != nil) {
        [self SetTimeLineIndex:currentview.tag-100];
    }
    
}

-(NSString *)GetRootPath
{
    NSString * rootpath = [[ResourceLoader getInstance] getModulePathByKey:moduleKey withAutoID:autoID];
    return rootpath;
}

-(NSArray *)GetFiles:(NSString *)path
{
    return [[ResourceLoader getInstance] getTemplatesByPath:path];
}

-(void)RefreshTimeLine:(NSArray *)array
{
    [timelineView RefreshByFavorite:array];
}

-(void)SetTimeLineAlpha:(NSNumber *)alpha
{
    timelineView.alpha = [alpha floatValue];
}

-(void)SetTimeLineIndex:(int)index
{
    [timelineView SetShowIndex:index];
}


-(void)dealloc
{
    [transView removeObserver:self forKeyPath:@"currentMiddleView"];
    self.autoID = nil;
    self.moduleKey = nil;
    [super dealloc];
}

@end
